<!DOCTYPE html>
<html>
<head>
    <%include("/back/_include/meta.html",{"title":""}){}%>
    <%include("/back/_include/style.html"){}%>
    <link href="/assets/ui/h+/css/plugins/iCheck/custom.css" rel="stylesheet">
</head>
<body class="gray-bg">
<div class="ibox float-e-margins">
    <div class="ibox-content">
        <div class="row">
            <div class="col-sm-6">
                <form role="form" id="form">
                    <div class="form-group">
                        <label>功能名称</label>
                        <input type="text" id="name" name="name" class="form-control" required="" minlength="2"
                               maxlength="20">
                        <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 2~20个字符以内</span>
                    </div>
                    <div class="form-group">
                        <label>url地址(要以'/'开头)</label>
                        <input type="text" id="url" name="url" class="form-control" required="" minlength="2"
                               maxlength="50">
                        <span class="help-block m-b-none"><i class="fa fa-info-circle"></i> 2~50个字符以内</span>
                    </div>
                    <div class="form-group">
                        <label>所属模块</label>
                        <span class="text-danger font-bold" data-id="${module.id!}">${module.name!}</span>
                        <button class="btn btn-sm btn-primary" type="button" id="chooseModule">选择</button>
                        <span class="help-block m-b-none"><i class="fa fa-info-circle"></i>请选择</span>
                    </div>
                    <div class="text-left">
                        <label>
                            <input type="checkbox" id="privilege" class="i-checks">
                            需要权限
                        </label>
                    </div>
                    <div>
                        <button class="btn btn-sm btn-primary" type="submit">
                            <strong>保存</strong>
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<%include("/back/_include/script.html"){}%>
<script src="/assets/ui/h+/js/plugins/validate/jquery.validate.min.js"></script>
<script src="/assets/ui/h+/js/plugins/validate/messages_zh.min.js"></script>
<script src="/assets/ui/h+/js/plugins/iCheck/icheck.min.js"></script>
<script>
    $.validator.setDefaults({
        submitHandler: function () {
            var name = $$('#name')
            var url = $$('#url')
            var moduleId = $('#chooseModule').prev().attr("data-id")
            var privilege = $("#privilege").prop("checked")?true:null;
            if(isEmpty(moduleId)){
                error("请选择模块")
                return
            }
            post("/b/operator/save", {
                "operator.name": name,
                "operator.url": url,
                "operator.module_id": moduleId,
                "operator.privilege": privilege
            }, function (res) {
                if (!res.success) {
                    error(res.msg)
                    return;
                }
                success("保存成功", false, function () {
                    window.location.href="/b/operator/add?moduleId="+moduleId;
                })
            }, function () {
                systemErr();
            })
        }
    });
    $(function () {
        $(".i-checks").iCheck({checkboxClass: "icheckbox_square-green", radioClass: "iradio_square-green",})
        $("#form").validate({
            messages: {
                name: {required: "请输入名称", minlength: "名称必须2个字符以上", maxlength: "名称必须20个字符以下"},
                url: {required: "请输入路径", minlength: "路径必须2个字符以上", maxlength: "路径必须50个字符以下"},
            }
        })
        $('#chooseModule').click(function () {
            ajaxHtml("/b/module/radio?id=" + $('#chooseModule').prev().attr("data-id"), function (res) {
                if (res.success === false) {
                    error("加载模块选择页面失败")
                    return;
                }
                configModal("#modal", "选择所属模块", res);
                $('#modal_btn').trigger("click");
            });
        })
    });
</script>
</body>
</html>